<?php

/**
 * This is the model class for table "{{mem_type}}".
 *
 * The followings are the available columns in table '{{mem_type}}':
 * @property integer $zmt_id
 * @property integer $zmg_id
 * @property string $zmt_title
 * @property string $zmt_content
 * @property string $zmt_type
 * @property integer $zmt_stat
 * @property string $zmt_pic
 * @property integer $zmt_min
 * @property integer $zmt_max
 * @property integer $inputtime
 * @property integer $updatetime
 */
class MemType extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return MemType the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{mem_type}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zmt_title', 'required'),
			array('zmg_id, zmt_stat, zmt_min, zmt_max, inputtime, updatetime', 'numerical', 'integerOnly'=>true),
			array('zmt_title', 'length', 'min'=>'4', 'max'=>100),
			array('zmt_content, zmt_pic', 'length', 'max'=>255),
			array('zmt_type', 'in', 'range'=>array('system','special','define')),
			array('zmt_content', 'safe'),
			//array('zmt_pic', 'file', 'types'=>'jpg,gif,png', 'maxSize'=>307200, 'allowEmpty'=>TRUE),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zmt_id, zmg_id, zmt_title, zmt_content, zmt_type, zmt_stat, zmt_pic, zmt_min, zmt_max, inputtime, updatetime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'memgroup' => array(self::BELONGS_TO ,'MemGroup','zmg_id'),               
			'actions'=>array(self::HAS_MANY, 'MemPrivilege', array('parent'=>'zmt_id'),'select'=>'child'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zmt_id' => 'ID',
			'zmg_id' => '会员组',
			'zmt_title' => '会员等级名称',
			'zmt_content' => '等级描述',
			'zmt_type' => '类型',
			'zmt_stat' => '状态',
			'zmt_pic' => '等级标志',
			'zmt_min' => '最小条件',
			'zmt_max' => '最大条件',
			'inputtime' => '建立时间',
			'updatetime' => '更新时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zmt_id',$this->zmt_id);
		$criteria->compare('zmg_id',$this->zmg_id);
		$criteria->compare('zmt_title',$this->zmt_title,true);
		$criteria->compare('zmt_content',$this->zmt_content,true);
		$criteria->compare('zmt_type',$this->zmt_type,true);
		$criteria->compare('zmt_stat',$this->zmt_stat);
		$criteria->compare('zmt_pic',$this->zmt_pic,true);
		$criteria->compare('zmt_min',$this->zmt_min);
		$criteria->compare('zmt_max',$this->zmt_max);
		$criteria->compare('inputtime',$this->inputtime);
		$criteria->compare('updatetime',$this->updatetime);

		$criteria->order = " inputtime DESC ";//默认为时间倒序
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
    //保存之前
	protected function beforeSave(){

	    if (parent::beforeSave()){	        
	        
	        if($this->isNewRecord){
	            $this->inputtime = $this->updatetime = time();
	        }else{
	        	$this->updatetime = time();
	        }	        
	        return true;
	    }else{
	        return false;
	    }
	}

	/**
	 * 取指定会员组的所有等级
	 * 如果无指定会员组，刚返回所有会员等级
	 */
	public function getMemtype($id=null)
	{
		if($id==null)
		{
			$MemtypeArr = MemType::model()->findAll();//取出所有的会员等级
		}else{
			$MemtypeArr = MemType::model()->findAllByAttributes(array('zmg_id'=>(int)$id));//出取指定会员组的等级
		}
		
      	return CHtml::listData($MemtypeArr, 'zmt_id', 'zmt_title');
	}

	/*
	* 取指定会员等级名称
	* @param int  $id 会员等级ID  
    */
	
   public function getMemTypeName($id)
   {
   		$MemTypeName = MemType::model()->findByAttributes(array('zmt_id'=>$id));

   		return $MemTypeName->zmt_title;
   }


}